package coe.pumbaa

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured

class CommentController {

    static allowedMethods = [save: "POST", update: "POST"]

    def stringManagerService
    def springSecurityService
    def authenticationManagerService
    
    def index() {
        redirect(controller:'home',action: "index")
    }

    def list() {
        def user
        if(springSecurityService.isLoggedIn()){
         user = springSecurityService.currentUser
        }else{
         user = authenticationManagerService.getAnonymousUser()
        }
        def post = Post.get(params.id)
        
        if(stringManagerService.checkObjectInString(user.role,post.category.allowView)){
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            def comments = Comment.findAll("from Comment as c where c.post=?",[post],params)
            [postInstance: post, 
                commentInstanceList: comments, 
                commentInstanceTotal: Comment.findAll("from Comment as c where c.post=?",[post]).size(),
                commentInstance: new Comment(), 
                navigation:[
                    pos:"post_title",
                    text:"comment",
                    params:["postID":post.id,"categoryID":post.category.id]
                ]
            ]
        }else{
            flash.warning = "You can not view post ("+post.title+")."
            redirect(controller:'home',action:'index')
        }
    }


    def save() {
        def user
        if(springSecurityService.isLoggedIn()){
         user = springSecurityService.currentUser
        }else{
         user = authenticationManagerService.getAnonymousUser()
        }
        def post = Post.get(params.postID)
        if(stringManagerService.checkObjectInString(user.role,post.category.allowView)){
            def commentInstance = new Comment()
            commentInstance.message = stringManagerService.stringFilter(params.message)
            commentInstance.timestamp = new Date()
            commentInstance.owner = user
            commentInstance.post = post
            if (!commentInstance.save(flush: true)) {
                flash.error = "Error! Can not comment."
            }else{
                flash.success = message(code: 'default.created.message', args: [message(code: 'comment.label', default: 'Comment'), commentInstance.id])
            }
            redirect(action: "list", id: params.postID)
        }else{
            flash.warning = "You can not post ("+post.title+")."
            redirect(controller:'home',action:'index')
        }
    }

    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def delete() {
        def user = springSecurityService.currentUser
        def commentInstance = Comment.get(params.id)
        def postID = commentInstance.post.id
        if(stringManagerService.checkObjectInString(user.role,commentInstance.post.category.allowView)){
            try {
                commentInstance.delete(flush: true)
                flash.success = message(code: 'default.deleted.message', args: [message(code: 'comment.label', default: 'Comment'), params.id])
                redirect(action: "list",id:postID)
            }
            catch (DataIntegrityViolationException e) {
                flash.error = message(code: 'default.not.deleted.message', args: [message(code: 'comment.label', default: 'Comment'), params.id])
                redirect(action: "list", id: postID)
            }
        }else{
            flash.warning = "You can not view post ("+post.title+")."
            redirect(controller:'home',action:'index')
        }
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def like(){
        def comment = Comment.get(params.id)
        def user = springSecurityService.currentUser
        if(stringManagerService.checkObjectInString(user.role,comment.post.category.allowView)){
            comment.likes = stringManagerService.addObjectInToString(springSecurityService?.currentUser,comment.likes)
            comment.save(flush:true)
            render(template: 'like' , model:[comment:comment])
        }else{
            flash.warning = "You can not view post ("+post.title+")."
            redirect(controller:'home',action:'index')
        }
    }
    
    @Secured(['IS_AUTHENTICATED_REMEMBERED'])
    def dislike(){
        def comment = Comment.get(params.id)
        def user = springSecurityService.currentUser
        if(stringManagerService.checkObjectInString(user.role,comment.post.category.allowView)){
            comment.likes = stringManagerService.deleteObjectFromString(springSecurityService?.currentUser,comment.likes)
            comment.save(flush:true)
            render(template: 'like' , model:[comment:comment])
        }else{
            flash.warning = "You can not view post ("+post.title+")."
            redirect(controller:'home',action:'index')
        }
    }
}
